@extends('layouts.appManager')

@section('title')
{{$server->name}} {{$server->server_host}}
@endsection

@section('style')

    <style>
        #app{
            font-size: 14px;
        }
    </style>
@endsection
@section('script')

    <script>
        let app = new Vue({
            el: '#app',
            data: {
                step:0,
                activeName:'p0',
                systemInfo:'',
                systemStatus:-1,   //-1：未知，0不支持   1支持
                installInfo:'',
                installStatus:-1,   //-1 未知  0未安装   1安装中  2宝塔安装成功  3全部成功
                btInfo:'',
                btStatus:-1,        //-1 未知   0对接失败    1对接成功
                runStatus:-1,       //-1 未知   0未运行   1运行中
                server:null,
                mods:[],
                port:[
                    false,//3306
                    false,//8085
                    false,//3724
                    false,//7878
                ]
            },
            mounted() {
                // layer.open({
                //     type: 2,
                //     area: ['90%', '90%'],
                //     content: 'http://wow.study/' //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                // });
                this.serverInfo();
            },

            watch:{
                // step(newValue, oldValue){
                //     this.activeName='p'+newValue;
                // },
                systemStatus(newValue, oldValue){
                    if(newValue===1) {
                        this.step=1;
                        this.getInstallInfo();
                    }
                },
                installStatus(newValue, oldValue){
                    if (newValue>1){
                        this.step=2;
                        // this.checkBT();
                        // if (this.server.bt_key){  //如果有宝塔key
                        // }else{
                        //     this.getBT();
                        // }
                        if (newValue===3){
                            this.step=3;
                            this.serverStatus()
                        }
                    }
                },
                runStatus(newValue, oldValue){
                    if (newValue===1) {   //服务器运行中
                        this.step=4;
                        this.checkPort(3306);
                        this.checkPort(3724);
                        this.checkPort(8085);
                        this.checkPort(7878);
                    }
                },
            },
            methods:{
                runCommand(command){
                    this.$confirm('确定重启?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        var t=this;
                        post("/manager/server/config/runGMCommand",{id:t.server.id,command:command},res=>{
                            if (res.data===1){
                                t.$message({
                                    type: 'success',
                                    message: '执行成功'
                                });
                            }else{
                                t.$message({
                                    type: 'error',
                                    message: '执行失败'
                                });
                            }
                        },t);
                    }).catch(() => {
                    });
                },
                serverInfo(){
                    post('ajaxServerInfo', {},res=>{
                        console.log(res);
                        this.server=res.data.server;
                        if (this.server.version==null || this.server.version==0) this.server.version=1;
                        // this.mods=res.data.mods;
                        // this.server.mods=[];
                        if (this.server.db_pwd){  //如果有通用密码
                            this.serverTest();
                        }
                    },this)
                },
                serverTest(){
                    post('ajaxServerTest',{},res=>{
                        console.log(res);
                        this.systemInfo=res.data;
                        if(res.res===1){
                            this.systemStatus=1
                        }else{
                            this.systemStatus=0
                            this.$message.error(res.msg);
                        }
                    },this)
                },
                getInstallInfo(){
                   post('ajaxInstallInfo',{},res=>{
                       console.log(res);
                       this.installInfo=res.data;
                       this.installStatus=res.res;
                   },this)
                },
                serverStatus(){
                    post('ajaxServerStatus',{},res=>{
                        console.log(res);
                        this.runStatus=res.res;
                    },this)
                },
                portIndex(port){
                    switch (port) {
                        case 3306:
                            return 0;
                        case 8085:
                            return 1;
                        case 3724:
                            return 2;
                        case 7878:
                            return 3;
                    }
                },
                checkPort(port){
                    this.$set(this.port, this.portIndex(port), false);
                    post('ajaxTelnet',{port:port},res=>{
                        console.log(res);
                        if (res.res===1){
                            // this.port[this.portIndex(port)]=true;
                            // this.$forceUpdate();
                            this.$set(this.port, this.portIndex(port), true);
                        }else{
                            this.$message.error("端口："+port+" 通信失败");
                        }

                    },this)
                },
/*                checkBT(){
                    post('ajaxCheckBT',{},res=>{
                        console.log(res);
                        this.btStatus=res.res;
                    },this)
                },
                getBT(){
                    post('ajaxGetBT',{},res=>{
                        console.log(res);
                        this.btInfo=res.data;
                    },this)
                },*/
                saveServer(){
                    if(!this.server.server_host || !this.server.root_pwd || this.server.server_host.trim()==='' || this.server.root_pwd.trim()===''){
                        this.$message.error('请输入完整信息');
                        return;
                    }
                    post('ajaxSaveServer',this.server,res=>{
                        console.log(res);
                        this.systemInfo=res.data;
                        if(res.res===1){
                            this.$message.success('保存成功');
                            this.server.db_pwd=res.msg;
                            this.serverTest();
                        }else{
                            this.$message.error(res.msg);
                        }
                    },this)
                },
                installNew(){
                    post('ajaxInstallNew',this.server,res=>{
                        console.log(res);
                        this.getInstallInfo();
                    },this)
                },
                serverUpdate(){
                    this.$confirm('更新前建议关闭服务器, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        post('ajaxServerUpdate',this.server,res=>{
                            console.log(res);
                            this.getInstallInfo();
                        },this)
                    }).catch(() => {
                    });
                },
                serverPull(){
                    post('ajaxServerPull',this.server,res=>{
                        console.log(res);
                        this.getInstallInfo();
                    },this)
                },
                installInfoRef(){
                    this.getInstallInfo();
                },

                runSQL(act){
                    post('ajaxRunSQL',{act:act},res=>{
                        console.log(res);
                        this.$message.success('安装成功，需要重启服务器');
                    },this)
                },

/*                saveBT(){
                    if(!this.server.bt_url || !this.server.bt_key || this.server.bt_url.trim()==='' || this.server.bt_key.trim()===''){
                        this.$message.error('请输入完整信息');
                        return;
                    }
                    post('ajaxSaveBT',this.server,res=>{
                        console.log(res);
                        this.btStatus=res.res;
                        if(res.res===1){
                            this.$message.success('保存成功');
                        }else if(res.data.data!=null){
                            this.$message.error(res.data.data.msg);
                        }else{
                            this.$message.error('对接失败，请检查地址和密钥');
                        }
                    },this)
                },*/
                serverStart(){
                    post('ajaxServerStart',{},res=>{
                        console.log(res);
                        this.serverStatus();
                    },this)
                },
                serverStop(){
                    this.$confirm('确定直接关闭?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        post('ajaxServerStop',{},res=>{
                            console.log(res);
                            this.serverStatus();
                        },this)
                    }).catch(() => {
                    });
                },
                // isBot(status){
                //     if (status)
                //         this.server.mods=['npcbot'];
                //     else
                //         this.server.mods=[];
                //     console.log(status);
                // }
            }
        });
    </script>
@endsection

@section('content')
<div class="container py-5">
    <div class="row justify-content-center">

        <div class="col-md-8 mt-5">
{{--@{{activeName}}
            <el-input-number v-model="step" ></el-input-number>
            <el-input-number v-model="installStatus" ></el-input-number>--}}
            <el-tabs v-model="activeName">
                <el-tab-pane :disabled="step<0" name="p0" label="1.系统检查">
                    <span slot="label">
                        1.系统检查 <i :class="systemStatus===1?'el-icon-success':'el-icon-error'"></i>
                    </span>
                    <div v-if="systemStatus===0">
                        <el-alert
                            title="服务器未开启或系统不支持"
                            type="warning">
                        </el-alert>
                        <br>
                        服务器信息： <div class="mt-1" v-html="systemInfo"></div><hr>

                    </div>
                    <div v-if="systemStatus<=0">
                        <el-form v-if="server" label-width="180px">
                            <el-form-item label="服务器ip">
                                <el-input v-model="server.server_host"></el-input>
                            </el-form-item>
                            <el-form-item label="root用户密码">
                                <el-input type="password" v-model="server.root_pwd"></el-input>
                            </el-form-item>
                            <el-form-item label="登录端口">
                                <el-input type="text" v-model="server.root_port" placeholder="默认 22"></el-input>
                            </el-form-item>
                            <!--                        <el-form-item label="通用密码">
                                                        <el-input :value="server.db_pwd" placeholder="不用输入自动生成" readonly></el-input>
                                                    </el-form-item>-->
                            <el-form-item>
                                <el-button type="primary" @click="saveServer" :disabled="installStatus === 1 || installStatus === 2">保存</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                    <div v-if="systemStatus===1">
                        <el-alert
                            title="服务器系统检查通过"
                            type="success">
                        </el-alert>
                        <br>
                        服务器信息： <div class="mt-1" v-html="systemInfo"></div><hr>
                    </div>

                </el-tab-pane>
                <el-tab-pane :disabled="step<1" name="p1" label="2.环境安装">
                    <span slot="label">
                        2.环境安装
                        <i v-if="installStatus===3" class="el-icon-success"></i>
                        <i v-else-if="installStatus>0" class="el-icon-more"></i>
                        <i v-else class="el-icon-error"></i>
                    </span>

                    <div v-if="installStatus===0">
                        <el-alert
                            title="系统未安装服务端，请保证系统为全新系统，点击全新安装"
                            type="warning">
                        </el-alert>

                        <br>
                        <el-select v-model="server.version" placeholder="请选择">
                            <el-option label="Azerothcore 旧版(精修无bug)" :value="1"></el-option>
                            <el-option label="Azerothcore 最新原版" :value="0"></el-option>
                            <el-option label="Azerothcore NPC机器人版" :value="2"></el-option>
                        </el-select><br><br>
                        版本对比，请查看：<a href="http://bbs.wowmanage.cn/d/21-fu-wu-duan-gong-neng-chai-bie-jie-shao" target="_blank"> http://bbs.wowmanage.cn </a>
                        <br><br>
                        <el-button type="primary" @click="installNew">全新安装</el-button>
                    </div>
                    <div v-else-if="installStatus===3">
                        <el-alert
                            title="服务端安装成功"
                            type="success">
                        </el-alert>
                        <br>
                        <el-row :gutter="20">
                            <el-col :span="8">
                                <el-button size="small" type="primary" @click="installNew">重新安装</el-button>

                                <br><br>
                                <div>
{{--                                    当前版本：{{$server->getVersion()}}--}}
{{--                                    <br>--}}
{{--                                    最新版本：{{ config("conf.CoreVer")['S'.$server->type]['C'.$server->version] }}--}}
{{--                                    <br><br>--}}
                                    <el-button size="small" type="primary" @click="serverUpdate">更新</el-button>
                                </div>

                            </el-col>
                            <el-col :span="16">
                                <el-collapse>
                                    <el-collapse-item title="敏感信息">
                                        <div>IP： {{$server->server_host}}</div>
                                        <hr>
                                        <div>服务器账号： root</div>
                                        <div>服务器密码： {{$server->root_pwd=='yun@789..com'?'**********':$server->root_pwd}}</div>
                                        <hr>
                                        <div>数据库账号： {{$server->db_acc}}</div>
                                        <div>数据库密码： {{$server->db_pwd=='g6HBFLKE3tTw'?'**********':$server->db_pwd}}</div>
                                        <hr>
                                        <div>管理员账号： {{$server->soap_acc}}</div>
                                        <div>管理员密码： {{$server->soap_pwd=='g6HBFLKE3tTw'?'**********':$server->soap_pwd}}</div>
                                    </el-collapse-item>
                                </el-collapse>
                                <br>
                                安装信息： <div class="mt-1" v-html="installInfo"></div>
                                <br>

                            </el-col>
                        </el-row>

                    </div>
                    <div v-else-if="installStatus<3">
                        <el-alert
                            title="服务端安装中，请耐心等待，根据服务器配置不同，可能需要30-60分钟。 安装期间，可以登录服务器，使用命令：tmux attach -t install 查看安装过程。"
                            type="warning">
                        </el-alert>
                        <br>

                        安装信息： <div class="mt-1" v-html="installInfo"></div><hr>
                        <el-button size="small" type="primary" @click="installNew">重新安装</el-button>
                        <el-button size="small" type="primary" @click="installInfoRef">刷新</el-button>
                    </div>


                </el-tab-pane>
{{--                <el-tab-pane :disabled="step<2" name="p2" label="3.设置宝塔">
                    <span slot="label">
                        3.设置宝塔 <i :class="btStatus===1?'el-icon-success':'el-icon-error'"></i>
                    </span>
                    <div v-if="btStatus===0">
                        <el-alert
                            title="宝塔对接失败：正常情况，以下内容无需填写，刷新页面，系统会自动获取"
                            type="error">
                        </el-alert>
                        <br>
                        <el-form v-if="server"  label-width="100px">
                            <el-form-item label="宝塔地址">
                                <el-input v-model="server.bt_url"></el-input>
                            </el-form-item>
                            <el-form-item label="宝塔接口密钥">
                                <el-input v-model="server.bt_key"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" @click="saveBT">保存</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                    <div v-else-if="btStatus===1">
                        <el-alert
                            title="宝塔对接成功"
                            type="success">
                        </el-alert>
                        <br>
                        <el-alert
                            title="请务必登录宝塔面板，绑定宝塔账号，否则部分功能无法正常使用"
                            type="warning">
                        </el-alert>
                        <br>
                        <el-button size="mini" @click="getBT">查看宝塔登录信息</el-button>
                        <br>
                    </div>
                    <div v-html="btInfo">
                    </div>


                </el-tab-pane>--}}
                <el-tab-pane :disabled="installStatus!==3" name="p3" label="3.服务管理">
                     <span slot="label">
                        3.服务管理 <i :class="runStatus===1?'el-icon-success':'el-icon-error'"></i>
                    </span>
                    <div v-if="runStatus===0">
                        <el-alert
                            title="服务端未运行"
                            type="warning">
                        </el-alert>
                        <br>
                        <el-button type="primary" @click="serverStart">运行服务端</el-button>
                    </div>
                    <div v-if="runStatus===1">
                        <el-alert
                            title="服务端运行中"
                            type="success">
                        </el-alert>
                        <br>
                        <el-row :gutter="20">
                            <el-col :span="12">
                                <div>
                                    刚运行的服务端，需要稍等片刻才可以连接， 如果要查看服务器启动信息，请登录服务器，输入命令：<br>
                                    tmux attach -t auth-session <br>
                                    tmux attach -t world-session
                                </div>
                                <br>
                                <el-button type="primary" @click="serverStop">关闭服务端</el-button>
{{--                                <el-button type="primary" @click="runCommand('server restart 30')">30秒重启服务端</el-button>  重启时，ACSoap: couldn't bind to 0.0.0.0:7878 导致启动失败--}}
                            </el-col>
                            <el-col :span="12">
                                如果服务器运营商有安全策略，请分别放行下列端口：<br> 如果检测失败，点击端口，重新检测<br><br>
                                <div class="mb-5">
                                    <el-button size="mini" round :type="port[0]?'success':'danger'" @click="checkPort(3306)">3306</el-button>
                                    <el-button size="mini" round :type="port[1]?'success':'danger'" @click="checkPort(8085)">8085</el-button>
                                    <el-button size="mini" round :type="port[2]?'success':'danger'" @click="checkPort(3724)">3724</el-button>
                                    <el-button size="mini" round :type="port[3]?'success':'danger'" @click="checkPort(7878)">7878</el-button>
                                </div>
                                <el-alert v-if="!port[0] ||!port[1] ||!port[2] ||!port[3]"
                                    title="开启服务器后，如果端口依然无法测试通过，请登录服务器查看安装信息，然后关闭服务端，后重新安装"
                                    type="warning">
                                </el-alert>
                            </el-col>
                        </el-row>
                    </div>

                    <br>

                </el-tab-pane>
                {{-- <el-tab-pane name="p4" label="4.内容管理">



                    <br>
                    <el-button type="primary" size="mini" @click="windowShow('{{route('server.config',['server'=>$server->id])}}?hide=1','玩家网站')">玩家网站</el-button>
                    <el-button type="primary" size="mini" @click="windowShow('{{route('server.lua',['server'=>$server->id])}}?hide=1','lua脚本')">lua脚本</el-button>
                    <el-button type="primary" size="mini" :disabled="!port[0]" @click="windowShow('{{route('server.mall',['server'=>$server->id])}}?hide=1','商城管理')">商城管理</el-button>
                    <el-button type="primary" size="mini" :disabled="!port[0]" @click="windowShow('{{route('server.editor',['server'=>$server->id])}}','内容编辑')">内容编辑</el-button>
                    <el-button type="primary" size="mini" :disabled="!port[3]||runStatus!==1" @click="windowShow('{{route('server.gm',['server'=>$server->id])}}?hide=1','GM后台')">GM后台</el-button>

                    <br><br><br>

   <el-button type="warning" size="mini" @click="runSQL('0_item_template')">物品名称汉化（安装需要30秒左右，请耐心等待）</el-button>
    <br><br>
                    <el-button type="warning" size="mini" @click="runSQL('1_vendorNPC')">安装物品售卖NPC （安装需要15秒左右，请耐心等待）</el-button>
    <br>
    <h5>NPC：id-名称</h5>

                    <div>
                         50101-武器1
                        | 50102-武器2
                        | 50103-武器3
                        | 50104-武器4
                        | 50105-武器5
                        | 50106-武器6  <br>
                         50201-布甲1
                        | 50202-布甲2
                        | 50203-布甲3
                        | 50204-布甲4
                        | 50205-布甲5
                        | 50206-布甲6
                        | 50207-布甲7 <br>
                         50301-皮甲1
                        | 50302-皮甲2
                        | 50303-皮甲3
                        | 50304-皮甲4
                        | 50305-皮甲5
                        | 50306-皮甲6 <br>
                         50401-锁甲1
                        | 50402-锁甲2
                        | 50403-锁甲3
                        | 50404-锁甲4
                        | 50405-锁甲5
                        | 50406-锁甲6 <br>
                         50501-板甲1
                        | 50502-板甲2
                        | 50503-板甲3
                        | 50504-板甲4
                        | 50505-板甲5
                        | 50506-板甲6
                        | 50507-板甲7
                        | 50508-板甲8 <br>
                         50601-饰品1
                        | 50602-饰品2
                        | 50603-饰品3
                        | 50604-饰品4
                        | 50605-饰品5 <br>
                         50701-宝石1
                        | 50702-宝石2
                        | 50703-宝石3 <br>
                         51301-商品1
                        | 51302-商品2
                        | 51303-商品3
                        | 51304-商品4
                        | 51305-商品5
                        | 51306-商品6
                        | 51307-商品7
                        | 51308-商品8 <br>
                         50801-盾牌
                        | 50901-圣物
                        | 51001-雕像
                        | 51101-图腾
                        | 51201-橙色 <br>
                         52001-背包
                        | 52002-雕文
                        | 52003-坐骑
                        | 52004-强化

                    </div>


        <br>
    @if(Auth::user()->is_admin == 1)
        <div>
            <el-button type="warning" size="mini" @click="runSQL('2_nobind1')">安装无拾取绑定的装备</el-button>
            <el-button type="warning" size="mini" @click="runSQL('2_nobind2')">卸载无拾取绑定的装备</el-button>
        </div>

    @endif

                </el-tab-pane>--}}
            </el-tabs>
        </div>
    </div>
</div>
@endsection
